<?php

class DepopayApp extends MemberbaseApp
{
	var $_order_mod;
	var $_ordergoods_mod;
	var $_deposit_account_mod;
	var $_deposit_record_mod;
	
	/* 构造函数 */
    function __construct()
    {
         $this->DepopayApp();
    }

    function DepopayApp()
    {
        parent::__construct();
		$this->_order_mod	= &m('order');
		$this->_ordergoods_mod = &m('ordergoods');
		$this->_deposit_account_mod	= &m('deposit_account');
		$this->_deposit_record_mod  = &m('deposit_record');
		
    }
    function index()
    {
		if(!$this->_deposit_account_mod->_check_pay_status($this->visitor->get('user_id')))
		{
			$this->show_message('deposit_pay_status_close', 'config_deposit', 'index.php?app=deposit');
			return;
		}
		
		$order_id = intval($_GET['order_id']);
		
		if(!$order_id) {
			$this->show_warning('no_such_order');
			return;
		}
		
		$order_info = $this->_order_mod->get(array(
			'conditions'	=> 'order_alias.order_id='.$order_id,
			'join'          => 'has_orderextm',
		));
		

		if(!$order_info || $order_info['status'] != 11 || $order_info['payment_code'] != 'deposit' || $order_info['buyer_id'] != $this->visitor->get('user_id')){
			$this->show_warning('order_error');
			return;
		}
		
		if(!IS_POST)
		{
			$order_info['ordergoods'] = $this->_ordergoods_mod->find(array('conditions'=>'order_id='.$order_id,'fields'=>'goods_id,goods_name'));
			
			$deposit_account = $this->_deposit_account_mod->get(array('conditions'=>'user_id='.$order_info['buyer_id']));
		
			/* 当前位置 */
        	$this->_curlocal(LANG::get('member_center'),   'index.php?app=member',
                         LANG::get('deposit'),         'index.php?app=deposit',
                         LANG::get('deposit_cashier')
			);
			$this->import_resource(array('script' => 'jquery.plugins/jquery.validate.js'));
			$this->assign('order_info', $order_info + array('surplus' => round($order_info['order_amount'] - $deposit_account['money'],2)));
			$this->assign('deposit_account', $deposit_account);
			$this->assign('alipaybank', $this->_get_bank_inc('alipaybank'));
			$this->assign('paynotice', sprintf(Lang::get('paynotice'), Conf::get('site_name')));
			$this->_config_seo('title', Lang::get('cashier') . ' - ' . Conf::get('site_title'));
			$this->display('depopay.index.html');
		}
		else
		{
			$paymethod = trim($_POST['paymethod']);
			$password = trim($_POST['password']);

			if(!$this->_deposit_account_mod->_check_account_password($password, $this->visitor->get('user_id'))){
				$this->show_warning('password_error');
            	return;
			}
			
			if(!empty($_POST['paymethod']) && in_array($paymethod, array('AllNetPay','BalancePay')))
			{
				/* 1.当余额不足时，用余额及网银在线支付*/
				if($paymethod == 'BalancePay') {
					$deposit_account = $this->_deposit_account_mod->get(array('conditions'=>'user_id='.$order_info['buyer_id']));
					$money = round($order_info['order_amount'] - $deposit_account['money'],2);
				}
				/* 2.不管余额够不够，全部用网银在线支付 */
				if($paymethod == 'AllNetPay') {
					$money = $order_info['order_amount'];
				}
				
				$this->rechargepay($order_info, $money);
				exit;
				
			}
			/* 全部用虚拟币余额支付 */
			else
			{
				$depopay_type    =&  dpt('outlay', 'buygoods');
				$result 		= $depopay_type->submit(array(
					'trade_info' => array('user_id'=>$this->visitor->get('user_id'),'party_id'=>$order_info['seller_id'],'amount'=>$order_info['order_amount']),
					'extra_info' => $order_info,
					'post'		 =>	$_POST,
				));
			
				if(!$result)
				{
					$this->show_warning($depopay_type->_get_errors());
					return;
				}
				
				/* 短信和邮件提醒： 买家已付款通知卖家 */
				$this->sendMailMsgNotify($order_info, array(
						'key' => 'toseller_online_pay_success_notify'
					),
					array(
						'key' => 'pay', 
						'body' => sprintf(Lang::get('sms_pay'), $order_info['order_sn'], $order_info['buyer_name'])
					)
				);
			
				header('Location:index.php?app=paynotify&order_id='.$order_id);
			}
		}
    }
	
	/* 充值完成后进行支付 */
	function rechargepay($order_info, $money)
	{
		$payment_code = trim($_POST['payment_code']);
			
		$payment_model =& m('payment');	
		if (!$payment_model->in_white_list($payment_code))
		{
			$this->show_warning('payment_disabled_by_system');

			return;
		}
		$payment_info  = $payment_model->get("payment_code ='{$payment_code}' AND store_id=0");
				
            	
		if (!$payment_info['enabled'])
		{
			$this->show_warning('payment_disabled');

			return;
		}
				
		$result = Psmb_init()->DepositApp_recharge($payment_code, $payment_info, array(
			'defaultbank'		=> trim($_POST['defaultbank']),
			'total_fee'		=> $money,
			'subject'		=> Lang::get('recharge'),
		));
		list($payment_form, $tradesn) = $result;
			
		/* 转到对应的业务实例，不同的业务实例用不同的文件处理，如购物，卖出商品，充值，提现等，每个业务实例又继承支出或者收入 */	
		$depopay_type    =&  dpt('income', 'recharge');
				
		$result 	= $depopay_type->submit(array(
			'trade_info' =>  array('tradesn'=>$tradesn, 'user_id'=>$this->visitor->get('user_id'), 'party_id'=>0, 'amount'=>$money),
			'extra_info' =>  array('is_online'=>1, 'payment_code'=> $payment_code, 'order_id'=> $order_info['order_id'],'after_notify'=>'payorder'),
			'post'		 =>	 $_POST,
		));

		/* 跳转到真实收银台 */
		$this->_config_seo('title', Lang::get('cashier'));
		$this->assign('payform', $payment_form);
		$this->assign('payment', $payment_info);
		header('Content-Type:text/html;charset=' . CHARSET);
		$this->display('deposit.payform.html');
	}
}

?>
